package com.example.paper.mvp.model;

import com.example.mvp.model.BaseEntity;
import com.example.mvp.model.BaseModel;
import com.example.paper.api.Api;
import com.example.paper.brean.HttpCode;
import com.example.paper.brean.LoginBean;
import com.example.paper.brean.UpLoadBean;
import com.example.paper.http.factory.ChangeFunction;
import com.example.paper.http.factory.HttpFactory;
import com.example.paper.http.factory.HttpType;
import com.example.paper.mvp.contract.LoginContract;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import okhttp3.MultipartBody;

public class LoginModel extends BaseModel implements LoginContract.LoginModel {


    public Observable<BaseEntity> requestLogin(Map<String, Object> map) {
        int code = (int) map.get("code");
        List<MultipartBody.Part> list = (List<MultipartBody.Part>) map.get("list");
        map.remove("list");
        map.remove("code");
        if (code == HttpCode.MUSERLOGIN) {
            //登录
            Observable<LoginBean> loginBeanObservable = HttpFactory.getInstance().factory(HttpType.TOKENSIGNTYPE)
                    .getRetrofit()
                    .create(Api.class)
                    .requestLogin(createBody(map));//map集合转换createBody
            return loginBeanObservable.map(new ChangeFunction<LoginBean>());//向上转型
        } else if (code == HttpCode.MUSERREG) {
            //注册
            Observable<LoginBean> loginBeanObservable = HttpFactory.getInstance().factory(HttpType.TOKENSIGNTYPE)
                    .getRetrofit()
                    .create(Api.class)
                    .requestReg(createBody(map));
            return loginBeanObservable.map(new ChangeFunction<LoginBean>());
        }else if (code == HttpCode.UPLOADIMGCODE){
            Observable<UpLoadBean> ob = HttpFactory.getInstance().factory(HttpType.UPLOADTYPE)//上传头像
                    .getRetrofit().create(Api.class)
                    .requestUpload(list);
            return ob.map(new ChangeFunction<UpLoadBean>());
        }
        return null;
    }

    @Override
    public Observable<BaseEntity> requestUpload(Map<String, Object> map) {
        int code = (int) map.get("code");
        map.remove("code");

        if (code == HttpCode.UPDATEUSERCODE){//修改用户信息
            Observable<BaseEntity> ob = HttpFactory.getInstance().factory(HttpType.TOKENSIGNTYPE)
                    .getRetrofit()
                    .create(Api.class)
                    .requestUpdate(createBody(map));
            return ob.map(new ChangeFunction<BaseEntity>());
        }
        return null;
    }

}

